package la.iok.hzsvn.lewin.movie.service;

import la.iok.hzsvn.lewin.movie.entity.MovieAssistImage;
import la.iok.hzsvn.lewin.movie.mapper.MovieAssistImageMapper;
import la.iok.hzsvn.lewin.mybatis.service.BaseServiceImpl;
import la.iok.hzsvn.share.annotations.NotNull;
import la.iok.hzsvn.share.annotations.NullElement;
import la.iok.hzsvn.share.annotations.Nullable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Service
public class MovieAssistImageServiceImpl extends BaseServiceImpl<MovieAssistImage, MovieAssistImageMapper,Long> implements MovieAssistImageService {
    private final MovieAssistFileService movieAssistFileService;
    public MovieAssistImageServiceImpl(MovieAssistImageMapper mapper, MovieAssistFileService movieAssistFileService) {
        super(mapper);
        this.movieAssistFileService = movieAssistFileService;
    }

    @Override
    public Long save(Long movieInstanceId, Long imgFileId, String imgPath) {
        MovieAssistImage entity = new MovieAssistImage();
        entity.setFilePath(imgPath);
        entity.setFileId(imgFileId);
        entity.setMovieInstanceId(movieInstanceId);
        insertSelective(entity);
        return entity.getId();
    }

    @Nullable
    @Override
    public String digest(@Nullable Long id) {
        MovieAssistImage img = select(id);
        if(img == null){
            return null;
        }
        return movieAssistFileService.digest(img.getFileId());
    }

    @Nullable
    @Override
    public String storePath(@Nullable Long id) {
        MovieAssistImage img = select(id);
        if(img == null){
            return null;
        }
        return movieAssistFileService.storePath(img.getFileId());
    }

    @Override
    public void downloadFile(@NotNull HttpServletResponse response, @NotNull Long id) {
        MovieAssistImage image = select(id);
        if(image == null){
            return;
        }
        movieAssistFileService.downloadFile(response,image.getFileId(),String.valueOf(id));
    }

    @Override
    public List<MovieAssistImage> assistImages(Long movieInstanceId) {
        if(movieInstanceId==null){
            return Collections.emptyList();
        }
        return listByProperty("movieInstanceId", movieInstanceId);
    }

    @Override
    public void markFileDeleted(@Nullable @NullElement Collection<Long> ids) {
        batchUpdate("fileDeleted",1,ids);
    }
}
